# -*- coding: utf-8 -*-
# vim: set noet ts=4:
#
# scim-python
#
# Copyright (c) 2007-2008 Yu Fan <yufanyufan@gmail.com>
#
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330,
# Boston, MA  02111-1307  USA
#
# $Id: $
#
import os
import os.path as path
import sys
import sqlite3 as sqlite
import traceback
import PYDict
from PYSQLiteDB import PYSQLiteDB, Cache
import re

(YLEN, Y0, Y1, Y2, Y3, YX, S0, S1, S2, S3, PHRASE, FREQ, USER_FREQ) = range (0, 13)


class ShuangPinDB (PYSQLiteDB):
	"""phrase database that contains all phrases and phrases' pinyin"""
	def __init__ (self):
		PYSQLiteDB.__init__ (self, user_db = "user_shuangpin.db")

		#~ Init caches
		self.select_cache = Cache ()
		self.select_all_cache = Cache()
		self.longest_length_cache = Cache()

	def clear_cache(self):
		self.select_cache.clear()
		self.select_all_cache.clear()
		self.longest_length_cache.clear()

	def add_phrase (self, pinyin, freq = 2000000, database = "user_db", user_freq=1):
		""" add phrase to database"""
		sqlstring = """INSERT INTO %s.py_phrase_%d (ylen, y0, y1, y2, y3, yx, s0, s1, s2, s3, phrase, freq, user_freq)
			VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"""
		phrase = u"".join([i.char for i in pinyin])
		#~ print "commit", phrase
		record = [None, None, None, None, None, None, None, None, None, None, None, 0, None]
		record [YLEN] = len (pinyin)
		i = 0
		for p in pinyin[:4]:
			record[Y0 + i] = p.get_pinyin_id ()
			record[S0 + i] = p.get_sheng_mu_id ()
			i += 1
		if len(pinyin) > 4:
			record[YX] = "'".join (map (str, pinyin[4:]))
		record[PHRASE] = phrase
		record[FREQ]= freq
		record[USER_FREQ] = user_freq
		self.db.execute (sqlstring % (database, record[S0]), record)
		self.db.commit ()
		del self.select_cache[u"'".join (map (str, pinyin))]

	def get_longest_phrase_length(self , pylist):
		"""return the longest word starting with first two pinyin of pylist"""
		if len(pylist)<3:
			return len(pylist)
		pys=pylist[:2]
		pinyin_string = u"'".join (map (str, pys))
		result = self.longest_length_cache [pinyin_string]
		if result != None:
			return result
		tables_union = """( SELECT ylen FROM main.py_phrase_%(i)d WHERE %(conditions)s UNION ALL
		SELECT ylen FROM user_db.py_phrase_%(i)d WHERE %(conditions)s )"""
		sql_conditions = []
		if len (pys) <= 4:
			i = 0
			for py in pys:
				if py.is_complete ():
					sql_conditions.append ("y%d = %d" % (i, py.get_pinyin_id ()))
				else:
					sql_conditions.append ("s%d = %d" % (i, py.get_sheng_mu_id ()))
				i += 1
		else:
			i = 0
			for py in pys[:4]:
				if py.is_complete ():
					sql_conditions.append ("y%d = %d" % (i, py.get_pinyin_id ()))
				else:
					sql_conditions.append ("s%d = %d" % (i, py.get_sheng_mu_id ()))
				i += 1
			pp = lambda (x): x.get_pinyin() if x.is_complete () else x.get_pinyin() + "%"
			pattern = "'".join (map (pp, pys[4:]))
			sql_conditions.append ("yx LIKE \"" + pattern + "\"")
		tables_union = tables_union % {"i" : pys[0].get_sheng_mu_id (), "conditions" : " AND ".join (sql_conditions) }
		sql_string = "SELECT Max(ylen) FROM " + tables_union + " ;"
		result = list (self.db.execute (sql_string).fetchall ())
		if not result[0][0]:
			result =  1
		else:
			result = result[0][0]
		self.longest_length_cache[pinyin_string] = result
		#~ print "longest",pinyin_string, result
		if result > len(pylist):
			return len(pylist)
		return result

	def select_words_by_phrase(self, pys, database=None):
		return self.select_words_by_pinyin(pys, phrase=u"".join([i.char for i in pys]),database=database)

	def select_words_by_pinyin(self,pys,length=None,phrase=None,database=None):
		sql_conditions = []
		if length:
			sql_conditions = ["ylen = %d" % length ]
		if phrase:
			sql_conditions.append("phrase = \"%s" % phrase +"\"")
		if database:
			tables_union = "( SELECT ylen, y0, y1, y2, y3, yx, phrase, freq, user_freq FROM " + database+ ".py_phrase_%(i)d WHERE %(conditions)s )"
		else:
			tables_union = """( SELECT ylen, y0, y1, y2, y3, yx, phrase, freq, user_freq FROM user_db.py_phrase_%(i)d WHERE %(conditions)s UNION ALL
		SELECT ylen, y0, y1, y2, y3, yx, phrase, freq, user_freq FROM main.py_phrase_%(i)d WHERE %(conditions)s )"""		
		if len (pys) <= 4:
			i = 0
			for py in pys:
				if py.is_complete ():
					sql_conditions.append ("y%d = %d" % (i, py.get_pinyin_id ()))
				else:
					sql_conditions.append ("s%d = %d" % (i, py.get_sheng_mu_id ()))
				i += 1
		else:
			i = 0
			for py in pys[:4]:
				if py.is_complete ():
					sql_conditions.append ("y%d = %d" % (i, py.get_pinyin_id ()))
				else:
					sql_conditions.append ("s%d = %d" % (i, py.get_sheng_mu_id ()))
				i += 1
			pp = lambda (x): x.get_pinyin() if x.is_complete () else x.get_pinyin() + "%"
			pattern = "'".join (map (pp, pys[4:]))
			sql_conditions.append ("yx LIKE \"" + pattern + "\"")
		tables_union = tables_union % {"i" : pys[0].get_sheng_mu_id (), "conditions" : " AND ".join (sql_conditions) }
		sql_prefix = "SELECT ylen, y0, y1, y2, y3, yx, phrase, MAX(freq*ifnull(user_freq,1)) as adj_freq FROM " + tables_union
		sql_string = sql_prefix + " GROUP BY phrase ORDER BY adj_freq DESC;"
		result = list (self.db.execute (sql_string).fetchall ())
		return result

	def select_words_by_pinyin_list_all (self, pys, length=None):
		"""select words from database by list that contains PYDict.PinYinWord objects"""
		pinyin_string = u"'".join (map (str, pys))
		result = self.select_all_cache [pinyin_string]
		if result == None:
			result = self.select_words_by_pinyin(pys)
			self.select_all_cache[pinyin_string] = result
			#~ print "list_all",pinyin_string
		return result
		
	def select_words_by_pinyin_list (self, pys):
		"""select words from database by list that contains PYDict.PinYinWord objects"""
		pinyin_string = u"'".join (map (str, pys))
		result = self.select_cache [pinyin_string]
		if result == None:
			result = self.select_words_by_pinyin(pys, len(pys))
			self.select_cache[pinyin_string] = result
			#~ print "list",pinyin_string
		return result
	
	def adjust_phrase_freq (self, pylist):
		"""this function adjusts frequence of phrase in user database."""
		p = self.select_words_by_phrase(pylist,"user_db")
		if len(p)>0:
			sql = "UPDATE user_db.py_phrase_%d SET user_freq = user_freq + 1 WHERE phrase = ?" % pylist[0].get_sheng_mu_id ()
			self.db.execute (sql, (u"".join([i.char for i in pylist]),))
			self.db.commit ()
		else:
			p = self.select_words_by_phrase(pylist,"main")
			self.add_phrase(pylist,p[0][7],user_freq=2)
